/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_note_send.js                                 
*  Description  : 쪽지발송
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-18  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	function clsNick(){
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsNick.prototype = {
		_init: function(){
			var $layer = $("strong.name");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setInfo({
				"닉네임": ""
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
			
			$layer.html(this.infoData["닉네임"]);
		}
	}
	
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		// 쪽지발송
		, noteSubmit: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "sendMessage";
			var options = {
				"recUserId": arg["아이디"]
				, "messageBody": encodeURIComponent(arg["내용"])
			}
			_gf.ajax({
				"sid": "MG001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishNoteSubmit(res);
			});
		}
	}
	
	
	function clsControl()
	{
		this.objComm = null;
		this.objNick = null;
		
		this.openerControl = null;
		
		this.q_id = null;
		this.q_re = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objNick = new clsNick();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
			
			if(opener){
				if(opener.document.objControl){
					this.openerControl = opener.document.objControl;
				}
			}
			
			if(!this.openerControl){
				self.close();
				return;
			}
			
			this.q_id = _util.getParam("q_id");
			this.q_re = _util.getParam("q_re");
			
			if(!this.q_id){
				alert("비 정상적 접근입니다.");
				self.close();
				return;
			}
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			$(document).on("click", "img[alt='close'], img[alt='취소'], img[alt='보내기']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "close":
					case "취소":
						self.close();
						break;
					case "보내기":
						thisClass.noteSubmit();
				}
			});
		}
		, _start: function(){
			var thisClass = this;
			
			// 회원정보
			_gf.spinStart();
			_gf.getMemberInfo({
				"아이디": this.q_id
				, "콜백": function(ret, info){
					_gf.spinEnd();
					thisClass.objNick.setInfo(info);
				}
			});
			
			_util.setWindowSize();
		}
		// 쪽지발송
		, noteSubmit: function(){
			var n = $("textarea").val();
			
			if(!n){
				alert("쪽지내용을 입력해 주십시요.");
				$("textarea").trigger("focus");
				return;
			}
			
			_gf.spinStart();
			this.objComm.noteSubmit({
				"아이디": this.q_id
				, "내용": n
			});
		}
		// 쪽지 발송 완료
		, finishNoteSubmit: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			var szLink = _ROOT + "/popup/pop_note_sent.html?q_re=" + this.q_re;
			document.location.href = szLink;
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});